package com.yoyo.itravel.web.controller;

import com.yoyo.itravel.bean.City;
import com.yoyo.itravel.bean.ResultInfo;
import com.yoyo.itravel.exceptions.CityException;
import com.yoyo.itravel.service.ICityService;
import com.yoyo.itravel.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 景点主题控制层
 * @Author HZX
 * @Version 1.0
 * @Date 2020/6/19 11:07
 */
@Controller
@RequestMapping("/city")
public class CityController {

    @Autowired
    // 声明景点主题服务层接口对象
    private ICityService cityService;

    /**
     * @param requset
     * @param response
     * @return void
     * @Description 通过省份id获得该省份下地所有城市
     * */
    @RequestMapping("/findCityByProvinceId")
    protected void findCityByProvinceId(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
        // 获得省份id
        String provinceId = requset.getParameter("provinceId");
        // 构建返回前端数据对象
        ResultInfo info = new ResultInfo();

        try {
            List<City> cities = cityService.findCityByProvinceId(provinceId);
            info.setFlag(true);
            JSONUtils.writeValueToResponse(cities, response);
        } catch (CityException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }
}
